<?php

/** 
 * 设备_控制器
 * @property User_dev_mdl $userDev
 * @property Dev_mdl $dev
 * @property School_mdl $school
 */
class Dev extends MY_Controller {
	
	public function __construct() {
		parent::__construct ();
		$this->user = parent::getLoginUser();
		
		$this->load->model('User_dev_mdl','userDev');
		$this->load->model('Dev_mdl','dev');
		$this->load->model('Tester_mdl','tester');
		$this->load->model('School_mdl','school');
	}
	/**
	 * 用户链接过的设备列表
	 */
	public function index(){
		
		
		$select = 'id,name,mac,alias,createTime,usedCount';
		$list  = $this->userDev->getDevListByUid($this->user->id,$select);
		$this->apiOutput->put('list', $list)->out();
	}
	/**
	 * 首次链接成功后纪录该设备
	 * 传入参数:
	 * 	mac:设备MAC
	 * 	name:设备名
	 *  alias:别名
	 */
	public function add(){
		$input = &$_REQUEST;
		if (empty ( $input ['name'] )) {
			$this->apiOutput->errorInput ();
		}
		$name = trim ( $input ['name'] );
		$mac = empty ( $input ['mac'] ) ? NULL : trim ( $input ['mac'] );
		$alias = empty ( $input ['alias'] ) ? NULL : trim ( $input ['alias'] );
		// 检查设备名是否存在并获取设备信息(查dev表)
		$where = array ();
		$where ['name'] = $name;
		$dev = $this->dev->getOne ( $where );
		if (empty ( $dev )) { // 如果没有纪录过,说明设备名未被重命名
			$this->apiOutput->error ( '设备未初始化,请等待工作人员初始化设备', - 2 );
		} else {
			$devID = $dev ['id'];
			if ($mac && empty ( $dev ['mac'] )) { // 如果之前没有传过mac,更新到dev表
				$update = array (
						'mac' => $mac 
				);
				$where = array (
						'id' => $dev ['id'] 
				);
				$this->dev->update ( $where, $update );
			}
		}
		// 检查该用户有没有链接过该设备(查user_dev表)
		$where = array ();
		$where ['userID'] = $this->user->id;
		$where ['devID'] = $devID;
		$recode = $this->userDev->getOne ( $where );
		if (empty ( $recode )) { // 如果没有链接过,纪录该设备
			$alias = empty ( $alias ) ? $name : $alias;
			$recode = array ();
			$recode ['devID'] = $devID;
			$recode ['userID'] = $this->user->id;
			$recode ['alias'] = $alias;
			$recode ['createTime'] = time ();
			$res = $this->userDev->insert ( $recode );
			if (! $res) {
				$this->apiOutput->errorSystem ();
			}
		} else if ($alias && $recode ['alias'] != $alias) { // 如果传入了别名且别名跟之前不一样,更新别名
			$this->_updateAlias ( $this->user->id, $recode ['devID'], $alias );
		} else {
			$alias = $recode ['alias'];
		}
		
		$dev ['id'] = ( int ) $devID;
		$dev ['alias'] = $alias;
		
		$this->apiOutput->put ( 'dev', $dev )->out ();
	}
	/**
	 * API:更新设备信息
	 * 传入参数: 
	 * 		devID:设备ID
	 * 		alias:设备别名
	 */
	public function update(){
		$devID = (int)$this->input->post_get('devID',TRUE);
		$alias = $this->input->post_get('alias',TRUE);
		if( empty($devID) || empty($alias) ){
			$this->apiOutput->errorInput();
		}
		$res = $this->_updateAlias($this->user->id, $devID, $alias);
		if ($res){
			$this->apiOutput->msg('修改成功!');
		}else{
			$this->apiOutput->errorSystem();
		}
	}
	/**
	 * 修改设备别名(同步更新订单表中的别名)
	 * @param int $userID
	 * @param int $devID
	 * @param string $alias
	 * @return bool
	 */
	private function _updateAlias($userID,$devID,$alias){
		$where = array();
		$where['userID'] = $userID;
		$where['devID']  = $devID;
		$update = array();
		$update['alias'] = $alias;
		
		$res = $this->userDev->update($where, $update);
		if( $res ){
			$this->load->model('Order_mdl','order');
			$this->order->update($where, $update);
		}
		return $res;
	}
	/**
	 * 初始化设备
	 * 传入参数:
	 * 	name:设备名
	 * 	mac
	 */
	public function init(){
		// 检查是否为测试人员
		if ( $this->user->isTest != 1 ) {
			$this->apiOutput->error('无操作权限',-1);
		}
		$where = array();
		$where['userID'] = $this->user->id;
		// 检查该测试账号是否已经失效
		$tester = $this->tester->getOne($where);
		if( time() > $tester['invalidTime'] ){
			$this->apiOutput->error('账号已失效',-2);
		}
		$schoolID = $tester['schoolID'];// 学校ID
		// 检测输入		
		$name = $this->input->post_get('name',TRUE);
		$mac = $this->input->post_get('mac',TRUE);
		$type = (int)$this->input->post_get('type');
		$name = trim($name);
		$mac  = trim($mac);
		if( empty($name)  ){
			$this->apiOutput->errorInput();
		}
		// 查找是否存在该名字的设备
		$where = array();
		$where['name'] = $name;
		$dev = $this->dev->getOne($where);
		if ( ! empty($dev) ) {
		    $this->apiOutput->error('该设备已初始化',-11);
		}
		// 如果不存在,生成新名称并插入
		$maxID = $this->school->incrementDevID($schoolID);// 获取设备在该校的最大ID
		if ( ! $maxID ) {
		    $this->apiOutput->errorSystem();
		}
		// 生成设备名
		$type   = $type === 2 ? 2 : 1;//默认为"饮水机"
		$prefix = $type === 2 ? 'RX' : 'RJ';
		$name = sprintf($prefix."%04s%04s",$schoolID,$maxID);  
		$dev = array();
		$dev['name']        = $name;
		$dev['mac']         = $mac;
		$dev['type']        = $type;
		$dev['schoolID']    = $schoolID;
		$dev['schoolDevID'] = $maxID;
		$dev['createTime']  = time();
		$devID = $this->dev->insert($dev);
		if ( ! $devID ) {
			$this->apiOutput->errorSystem();
		}
		$this->apiOutput->put('status', 1)->put('name', $name)->out();
	}

	
	
	
}

